package com.bawei.user

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.mvvm.BaseActivity
import com.bawei.base.room.UserUtils
import com.bawei.user.databinding.ActivityLoginBinding
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils


@Route(path = "/user/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun getLoutyId(): Int {
        return R.layout.activity_login
    }

    override fun getViewModel(): Class<UserViewModel> {
        return UserViewModel::class.java
    }

    override fun initView() {
        //点击获取验证码发送验证码
        binding.getyzm.setOnClickListener {
            viewModel.sendCode(binding.tel.text.toString())
        }

        //点击验证码登录
        binding.btn1.setOnClickListener {
            viewModel.loginCode(binding.yzm.text.toString(),binding.tel.text.toString())
        }

        //
        val spannableStringBuilder = SpannableStringBuilder(binding.xy.text.toString())
        val clickableSpan = object :ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/user/WebActivity")
                    .withSerializable("url","https://www.baidu.com").navigation()
            }
        }
        //
        spannableStringBuilder.setSpan(clickableSpan,4,10,Spannable.SPAN_INTERMEDIATE)
        binding.xy.movementMethod = LinkMovementMethod.getInstance()
        binding.xy.text = spannableStringBuilder


    }

    @SuppressLint("NewApi")
    override fun initDate() {
        viewModel.sendCodeSuccess.observe(this){
            if (it.code == 200){
                //截取验证码
                binding.yzm.setText(it.data.content.substring(8,14))
                //验证码通知
                val manage = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                manage.createNotificationChannel(NotificationChannel("1","1",NotificationManager.IMPORTANCE_HIGH))
                val build = Notification.Builder(this, "1")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("验证码")
                    .setContentTitle(it.data.content.substring(8, 14))
                    .build()
                manage.notify(1, build)
            } else {
                //请求失败吐司原因
                ToastUtils.showLong(it.message)
            }
        }
        //
        viewModel.sendCodeFail.observe(this){
            ToastUtils.showLong(it)
        }
        //
        viewModel.sendCodeFail.observe(this){
            ToastUtils.showLong(it)
        }

        //登录成功
        viewModel.loginCodeSuccess.observe(this){
            if (it.code == 200){
                ToastUtils.showLong("登录成功")
                //
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("tel",it.data.phone)
                //
                UserUtils.getDB().getUser().insert(it.data)
                //
                ARouter.getInstance().build("/user/UserInfoActivity").navigation()
                //
//                startActivity(Intent(this@LoginActivity,UserInfoActivity::class.java))
            }
        }
        //登录失败
        viewModel.loginCodeFail.observe(this){
            ToastUtils.showLong(it)
        }


    }

}